home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / GNUChess4_0_58.lha / gnuchess-4.0 / src / new / postprint.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  5KB  |  216 lines

  1. /*
  2.  * postprint.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1992 Free Software Foundation
  5.  *
  6.  * This file is part of GNU CHESS.
  7.  *
  8.  * GNU Chess is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2, or (at your option)
  11.  * any later version.
  12.  *
  13.  * GNU Chess is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with GNU Chess; see the file COPYING.  If not, write to
  20.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22. #include <stdio.h>
  23. #include "gnuchess.h"
  24. #define HASHFILE "/usr/local/lib/gnuchess.hash"
  25. #ifdef MSDOS
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #define RWA_ACC "r+b"
  30. #define WA_ACC "w+b"
  31. #else
  32. #define RWA_ACC "r+"
  33. #define WA_ACC "w+"
  34. #include <sys/param.h>
  35. #include <sys/types.h>
  36. #endif /* MSDOS */
  37. FILE *hashfile;
  38.  
  39. #define truescore 0x0001
  40. #define lowerbound 0x0002
  41. #define upperbound 0x0004
  42. #define kingcastle 0x0008
  43. #define queencastle 0x0010
  44.  
  45. struct fileentry
  46. {
  47.   unsigned char bd[32];
  48.   unsigned char f, t, flags, depth, sh, sl;
  49. };
  50. long i, j;
  51. int nr[MAXDEPTH];
  52. struct fileentry n;
  53. int r, c;
  54. char line[128];
  55. char *l;
  56. short int t;
  57. int cc1, cc2;
  58. char mvstr[3][6];
  59. short int board[64];
  60. void
  61. algbr (short int f, short int t, short int flag)
  62.  
  63. /*
  64.  * Generate move strings in different formats.
  65.  */
  66.  
  67. {
  68.   int m3p;
  69.  
  70.   if (f != t)
  71.     {
  72.       /* algebraic notation */
  73.       mvstr[0][0] = Cxx[column (f)];
  74.       mvstr[0][1] = Rxx[row (f)];
  75.       mvstr[0][2] = Cxx[column (t)];
  76.       mvstr[0][3] = Rxx[row (t)];
  77.       mvstr[0][4] = mvstr[3][0] = '\0';
  78.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  79.     {
  80.       if (mvstr[0][0] == mvstr[0][2])
  81.         {            /* pawn did not eat */
  82.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  83.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  84.           m3p = 2;
  85.         }
  86.       else
  87.         /* pawn ate */
  88.         {
  89.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* from column */
  90.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  91.           mvstr[2][2] = mvstr[0][3];
  92.           m3p = 3;        /* to row */
  93.         }
  94.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  95.       if (flag & promote)
  96.         {
  97.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  98.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  99.         }
  100.     }
  101.       else
  102.     /* not a pawn */
  103.     {
  104.       mvstr[2][0] = mvstr[1][0];
  105.       mvstr[2][1] = mvstr[0][1];
  106.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  107.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  108.       mvstr[2][4] = mvstr[1][3] = '\0';
  109.       strcpy (mvstr[3], mvstr[2]);
  110.       mvstr[3][1] = mvstr[0][0];
  111.       if (flag & cstlmask)
  112.         {
  113.           if (t > f)
  114.         {
  115.           strcpy (mvstr[1], "o-o");
  116.           strcpy (mvstr[2], "O-O");
  117.         }
  118.           else
  119.         {
  120.           strcpy (mvstr[1], "o-o-o");
  121.           strcpy (mvstr[2], "O-O-O");
  122.         }
  123.         }
  124.     }
  125.     }
  126.   else
  127.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  128. }
  129.  
  130. int
  131. main (int argc, char **argv)
  132. {
  133.   int f = 0;
  134.   char flbuf[10];
  135.   char *fl;
  136.  
  137.   if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  138.     exit (1);
  139.   for (i = 0; i < MAXDEPTH; i++)
  140.     nr[i] = 0;
  141.   fseek (hashfile, 0L, SEEK_END);
  142.   i = ftell (hashfile) / sizeof (struct fileentry);
  143.   fseek (hashfile, 0L, SEEK_SET);
  144.   printf ("/V 11 72 mul def /L 60 def\n");
  145.   for (j = 0; j < i; j++)
  146.     {
  147.       fread (&n, sizeof (struct fileentry), 1, hashfile);
  148.       if (n.depth)
  149.     {
  150.       nr[0]++;
  151.       if (nr[0] == 19)
  152.         {
  153.           nr[0] = 1;
  154.           printf ("showpage\n/V 11 72 mul def\n");
  155.           printf ("/L 60 def\n");
  156.           f = 0;
  157.         }
  158.       /* now process this entry */
  159.       strcpy (line, "C ('#[");
  160.       for (r = 0; r < 8; r++)
  161.         {
  162.           l = line + 6 + (7 - r) * 9;
  163.           for (c = 0; c < 4; c++)
  164.         {
  165.           cc1 = (n.bd[r * 4 + c] >> 4) & 0xf;
  166.           cc2 = n.bd[r * 4 + c] & 0xf;
  167.           board[r * 8 + c * 2] = (int) cc1 & 0x7;
  168.           board[r * 8 + c * 2 + 1] = (int) cc2 & 0x7;
  169.           if (cc1 & 0x8)
  170.             *l++ = Qxx[cc1 & 0x7];
  171.           else
  172.             *l++ = Pxx[cc1 & 0x7];
  173.           if (cc2 & 0x8)
  174.             *l++ = Qxx[cc2 & 0x7];
  175.           else
  176.             *l++ = Pxx[cc2 & 0x7];
  177.         }
  178.           *l++ = ';';
  179.         }
  180.       l--;
  181.       line[79] = '\0';
  182.       strcat (line, "]') show");
  183.       algbr (n.f, n.t, 0);
  184.       t = (n.sh << 8) + n.sl;
  185.       /* decode flags */
  186.       fl = flbuf;
  187.       if (n.flags & kingcastle)
  188.         *fl++ = 'k';
  189.       if (n.flags & queencastle)
  190.         *fl++ = 'q';
  191.       if (n.flags & truescore)
  192.         *fl++ = 't';
  193.       if (n.flags & lowerbound)
  194.         *fl++ = 'l';
  195.       if (n.flags & upperbound)
  196.         *fl++ = 'u';
  197.       *fl = '\0';
  198.       printf ("L V moveto\n");
  199.       printf ("R (%s flags %s depth %d score %d", mvstr[0], flbuf, n.depth, t);
  200.       printf (") show\n");
  201.       printf ("L  V 100 sub moveto\n");
  202.       printf ("%s\n", line);
  203.       f++;
  204.       if (f == 3)
  205.         {
  206.           printf ("/V V 120 sub def /L 60 def\n");
  207.           f = 0;
  208.         }
  209.       else
  210.         printf ("/L 160 L add def\n");
  211.     }
  212.     }
  213.   if (nr[0])
  214.     printf ("showpage\n");
  215. }
  216.